package com.td.client.log;

import org.newdawn.slick.Input;

import com.td.client.gui.GameRenderer;
import com.td.client.states.GameplayState;
import com.td.client.states.GameplayState.MODE;
import com.td.core.communication.Communication;
import com.td.core.globals.ObjectsGlobal.TowerType;
import com.td.core.obj.Player;
import com.td.core.obj.TDGame;
import com.td.core.obj.TDMap;
import com.td.core.obj.TDPoint;
import com.td.core.obj.logic.TowerLogic;
import com.td.core.obj.tower.Tower;

public class UserActionController {
	
	private Communication communication;
	private Player player;
	private Input input;
	
	private MODE mode;
	private TowerType addTowerType;
	
	private boolean action = false;
	
	private GameplayState gameplay;
	
	private int playerNumber;
	
	private static final int MARGIN = 10;
	private static final int MOVE_SPEED = 10;
	
	public UserActionController(Communication comm, Player player, int playerNumber, GameplayState gameplay) {
		this.playerNumber = playerNumber;
		this.communication = comm;
		this.player = player;
		this.gameplay = gameplay;
		this.input = gameplay.getGameContainer().getInput();
		
		setMode(MODE.DEFAULT);
	}
	
	public void update() {
		
		moveScreen();
		
		int mouseX = input.getMouseX() + GameRenderer.getInstance().getBaseX();
		mouseX -= playerNumber * TDMap.DrawParams.BSIZE_X + TDMap.DrawParams.BREAK;
		mouseX /= TDMap.DrawParams.FIELD_SIZE;
		
		int mouseY = input.getMouseY() + GameRenderer.getInstance().getBaseY();
		mouseY /= TDMap.DrawParams.FIELD_SIZE;
		
		switch (mode) {
			case ADD_TOWER:
				
				addTowerProcess(mouseX, mouseY);
				break;

			default:
				break;
		}
		
		if(isMouseLeftPressed(3))
			gameplay.setDefaultMode();
		
		send();
	}
	
	private void moveScreen() {
		GameRenderer gr = GameRenderer.getInstance();
		int mx = input.getMouseX();
		if(mx < MARGIN && gr.getBaseX() >= MOVE_SPEED) {
			gr.setBaseX(gr.getBaseX() - MOVE_SPEED);
			return;
		}
		if(mx > TDMap.Screen.X - MARGIN && gr.getBaseX() <= 
			TDMap.DrawParams.BSIZE_X * TDGame.MAX_PLAYERS - MOVE_SPEED - TDMap.Screen.X ) {
			gr.setBaseX(gr.getBaseX() + MOVE_SPEED);
			return;
		}
	}
	
	protected void send() {
		
		if(action) {
			communication.send(player.getCopy());
			action = false;
		}
	}
	
	private void addTowerProcess(int mouseX, int mouseY) {
		
		Tower tower = gameplay.getGameplayInfo().getAddingTower();
		tower.setPosition(new TDPoint(mouseX, mouseY));
		
		if(TowerLogic.canAddTower(player, tower)) 
			gameplay.getGameplayInfo().setTowerAddOn(true);
		else
			gameplay.getGameplayInfo().setTowerAddOn(false);
		
		if(isMouseLeftPressed(1)) {
					
			TowerLogic.addTower(player, tower);
		}
	}
	
	private boolean isMouseLeftPressed(int button) {
		
		switch(button){
		case 1:
			if(input.isMousePressed(Input.MOUSE_LEFT_BUTTON)){
				action = true;
				return true;
			}
			break;
		case 2:
			if(input.isMousePressed(Input.MOUSE_MIDDLE_BUTTON)){
				//action = true;
				return true;
			}
			break;
		case 3:
			if(input.isMousePressed(Input.MOUSE_RIGHT_BUTTON)){
				//action = true;
				return true;
			}
			break;
		}
		
		
		return false;
	}
	
	
	public MODE getMode() {
		return mode;
	}

	public void setMode(MODE mode) {
		this.mode = mode;
	}

	public TowerType getAddTowerType() {
		return addTowerType;
	}

	public void setAddTowerType(TowerType addTowerType) {
		this.addTowerType = addTowerType;
	}
	
}
